<template>
    <div>
        <x-header :left-options="{backText: ''}">发起活动</x-header>

        <group title="填写活动信息">
            <x-input class="black" title="名称" placeholder="活动名称" v-model="name" required></x-input>

            <x-input class="black" title="地点" placeholder="活动地点" v-model="place" required></x-input>

            <x-input class="black" title="预算/人" placeholder="每人预算" v-model="budget" required></x-input>

            <x-input class="black" title="人数" placeholder="人数" v-model="personNum" required></x-input>

            <datetime format="YYYY-MM-DD HH:mm" :placeholder="('请选择')" v-model="startDate"
                      :title="('开始时间')"></datetime>

            <selector title="类别" :options="list" v-model="type"></selector>

            <x-textarea :max="30" placeholder="请填写活动说明" v-model="introduction"></x-textarea>

        </group>
        <toast v-model="show" type="text">发起活动成功,待管理员审核~</toast>
        <toast v-model="show1" type="warn">{{msg}}</toast>
        <box gap="10px 10px">
            <x-button type="primary" @click.native="submit">确认</x-button>
        </box>
    </div>
</template>

<script>
    import {
        XInput, Group, XButton,
        Cell, Datetime, XHeader, Selector,
        XTextarea, Divider, Box, Toast
    } from 'vux'
    import request from 'superagent'

    export default {
        components: {
            XInput,
            XButton,
            Group,
            Cell,
            Datetime,
            XHeader,
            Selector,
            XTextarea,
            Divider,
            Box,
            Toast
        },
        data () {
            return {
                show: false,
                show1: false,
                name: '',
                place: '',
                personNum: 0,
                budget: 0.0,
                introduction: '',
                startDate: '',
                type: 'JYJH',
                msg: '网络开小差了~',
                list: [{key: 'JYJH', value: '交友聚会'},
                    {key: 'YYXJ', value: '音乐戏剧'},
                    {key: 'TYYD', value: '体育运动'},
                    {key: 'HWLX', value: '户外旅行'},
                    {key: 'JZGY', value: '讲座公益'},
                    {key: 'QT', value: '其他'}]
            }
        },
        methods: {
            submit () {
                if (this.name == '' || this.place == '' || this.budget == '' ||
                    this.personNum == '' || this.introduction == '' || this.startDate == ''
                    || this.type == '') {
                    this.msg = '请填写正确信息';
                    this.show1 = true
                } else {
                    request.post('/wx/activity/sponsoract')
                        .send({
                            name: this.name,
                            place: this.place,
                            budget: this.budget,
                            personNum: this.personNum,
                            startDate: this.startDate,
                            type: this.type,
                            introduction: this.introduction
                        })
                        .set('Content-Type', 'application/x-www-form-urlencoded')
                        //.set('Accept', 'application/json')
                        .end((err, res) => {
                            console.log(res);
                            const data = JSON.parse(res.text).data;
                            if (!res.ok) {
                                this.msg = '请求失败';
                                if (res.status == 400) this.msg = "填写信息有误"
                                this.show1 = true;
                            } else {
                                if (JSON.parse(res.text).code == '1') {
                                    this.msg = JSON.parse(res.text).resMsg;
                                    this.show1 = true;
                                } else {
                                    this.show = true;
                                }
                                setTimeout(() => {
                                    this.reload();
                                }, 2000);
                            }
                        });
                }
            },
            reload () {
                window.location.href = '/wechat/index.html';
            }
        }
    }

</script>
<style scoped>
    .weui_cell_ft .weui_btn {
        margin-left: 5px;
        vertical-align: middle;
        display: inline-block;
    }

    .red {
        color: red;
    }

    .black {
        color: #000000;
    }

    .green {
        color: green;
    }

</style>
